package com.xm.factory;

/**
 * 工厂方法模式
 */
public class BFactory {
    public static void main(String[] args) {
        LoggerFactory factory = new FileLoggerFactory();
        Logger logger = factory.createLogger();
        logger.log("测试日志");
    }
}
//1、产品接口
interface Logger{
    void log(String message);
}
//2、具体产品
class FileLogger implements Logger{
    @Override
    public void log(String message) {
        System.out.println("文件日志: " + message);
    }
}
class ConsoleLogger implements Logger{
    @Override
    public void log(String message) {
        System.out.println("控制台日志: " + message);
    }
}
//3、工厂接口
interface LoggerFactory{
    Logger createLogger();
}
//4、具体工厂
class FileLoggerFactory implements LoggerFactory{
    @Override
    public Logger createLogger() {
        return new FileLogger();
    }
}
class ConsoleLoggerFactory implements LoggerFactory{
    @Override
    public Logger createLogger() {
        return new ConsoleLogger();
    }
}
